package org.apselex.handlers;

/*
 * Copyright (c) 2008 Evan Williams
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.IOException;

import nu.xom.Document;
import nu.xom.Element;

import org.apache.log4j.Logger;
import org.apselex.ExportJob;
import org.apselex.model.ModelElement;
import org.apselex.providers.DataProvider;
import org.apselex.providers.XmlTranslator;
import org.apselex.views.ExplorerView;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.jface.dialogs.MessageDialog;

import apselexport.Activator;

/**
 * This class is a handler for the Import Command.
 * 
 * It extends AbstractHandler, an IHandler base class.
 * 
 * @see org.eclipse.core.commands.IHandler
 * @see org.eclipse.core.commands.AbstractHandler
 */
public class ImportHandler extends AbstractHandler {

	private static Logger logger = Logger.getLogger(ImportHandler.class);

	/**
	 * The constructor.
	 */
	public ImportHandler() {
	}

	/**
	 * execute the Import command.
	 * <p>
	 * this involves -
	 * </p>
	 * <ol>
	 * <li>prompting the user for the catalog file</li>
	 * <li>if the file is specified, open it and refresh the view</li>
	 * </ol>
	 */
	public Object execute(ExecutionEvent event) throws ExecutionException {

		logger.debug("execute import");

		// obtain a reference to the shell

		IWorkbenchWindow window = HandlerUtil
				.getActiveWorkbenchWindowChecked(event);
		Shell shell = window.getShell();

		FileDialog dialog = new FileDialog(shell, SWT.OPEN);

		final String file = dialog.open();
		if (file != null) {
			logger.debug("file is:" + file);

			// configure data provider - using the file name just obtained

			DataProvider data = Activator.getDefault().getDataProvider();
			data.setDatabaseName(file);

			// find the view, and refresh it

			ExplorerView view = (ExplorerView) window.getActivePage().findView(
					ExplorerView.ID);

			logger.debug("view is:" + view);
			if (view != null) {
				view.refresh();
			}
		}
		return null;
	}
}
